import time
import os
import json
import re

from DrissionPage import ChromiumPage, ChromiumOptions
from chat_dify_blocking import get_text

co = ChromiumOptions().set_paths(browser_path=r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe") # 设置浏览器路径co.set_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')  # 设置 User-Agent
# co.set_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')  # 设置 User-Agent
co.set_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36')  # 设置 User-Agent


page = ChromiumPage(co)

# 拿到所有商品链接和商品名称
# page.get('https://ixspy.com/data#/product/product-hot')
page.get('https://ixspy.com/data#/product/product-recommend')


# all_goods = page.eles('//li[@class="displayinline water-index-li water-li"]')
all_goods = page.eles('x://li[contains(@class, "water-index-li")]')

all_goods_dict = []
li_text = []
# print(f"all_goods{all_goods}")

for goods in all_goods:

    good = goods.ele('x://div[@class="el-tooltip water-li-div-title"]')

    goods_text = good.text

    # li_text.append(goods_text)
    if goods_text:
        # input = get_text(
        #     'Use a phrase to summarize the selling point, target group, and usage scenarios of this paragraph respectively.',
        #     goods_text)
        # print(f'input:{input}')
        selling_points = get_text('Use a phrase to summarize the selling point of this paragraph respectively',goods_text)
        print(f'selling_points:{selling_points}')
        time.sleep(1)

        target_group = get_text('Use a phrase to summarize the target_group of this paragraph respectively',goods_text)
        print(f'target_group:{target_group}')
        time.sleep(1)

        usage_scenarios = get_text('Use a phrase to summarize the usage scenarios of this paragraph respectively',goods_text)
        print(f'usage_scenarios:{usage_scenarios}')
        time.sleep(1)

        product_name = get_text('Extract product names from the following paragraph using one or two words:',goods_text)
        print(f'product_name:{product_name}')
        time.sleep(1)

        output = get_text('Generate a sales script for this product targeting a US audience of up to 100 words [product]:',product_name)
        print(f'output:{output}')
        time.sleep(1)

        goods_dict = {
            "instruction": f"Generate a sales script for [{product_name}] targeting American audiences",
            "input": f"Selling Points:{selling_points};Target Group:{target_group};Usage Scenarios:{usage_scenarios}",
            "output": f"{output}"
        }
        all_goods_dict.append(goods_dict)

# print(li_text)
# li = ['1~4PCS Solar Lights Outdoor With 3 Modes Waterproof Motion Sensor Security Lighting LED Wall Street Lamp for Garden 108/117COB', '72/108 COB Sensor Solar Lights Outdoor LED Street Garden Solar Lamps 3Modes Waterproof Motion Sensor Remote Control Wall Light', 'Solar LED Light, Outdoor Waterproof Wall Lamp, Dusk to Dawn Fence Lighting for Summer Garden Yard Porch Walkway Camping Home Dec', 'Outdoor Solar Tungsten Wall Light with Motion Sensor IP65 Waterproof LED Safety Light for Patio Outdoor Deck Porch Barn Garage', 'Motion Sensor Solar Tungsten Night Lamp Intelligent Wall Lamp 3 Lighting Modes Waterproof Courtyard Garden Decoration Lantern', 'Outdoor Solar Tungsten Wall Light with Motion Sensor IP65 Waterproof LED Safety Light for Patio Outdoor Deck Porch Barn Garage', 'Solar Outdoor Led Courtyard Wall Lamp Human Body Induction Garden Terrace Garage Door Street Lamp 616', 'AmmToo COB Outdoor Solar Lights Motion Sensor Solar 3 Modes Waterproof Security Wall Lighting for Patio Garden Door Street Light', 'Solar Lights Outdoor With 3 Mode Waterproof Motion Sensor Security Lighting Wall COB Street Lamp for Garden Patio Garage Light', 'Galaxy Projector Led Night Light Star Projector Astronaut Projector Galaxy Light for Home Decorative Bedroom Children Kids Gift', '4/6/8PCS 108 COB Sensor Street 3 Light Modes Outdoor Waterproof Security Solar Lamps for Garden Patio Path Remote Control Light', 'Remote Control Atmosphere Light Robot 360° Rotating Night Light Stepless Dimming Projector Night Lamp Net Red Selfie Light', 'Clearance_Photo Studio_Continuous updates', '1 Pc Solar Curtain Lights,300/200/100LEDs,Outdoor/Indoor Waterproof Fairy Lights, For Festival New Year LED Lights Christmas', 'Solar Outdoor Led Courtyard Wall Lamp Human Body Induction Garden Terrace Garage Door Street Lamp 616', '10-20Pcs Microfiber Towels Car Wash Drying Cloth Towel Household Cleaning Cloths Auto Detailing Polishing Cloth Home Clean Tools', 'Car Wash High End Microfiber Towel Car Cleaning Drying Cloth Hemming Car Care Cloth Detailing Wash Towel', 'Car cleaning tools cleaning brush Wool Soft Auto Car Washing Gloves Motorcycle Washer Care Car paint Wash Tools Accessories', 'Microfiber Car Cleaning Towel 600GSM Wash Towels Car Double-Sided Soft Cloth Auto Detailing Care Washing Drying Rags', 'G40 LED Outdoor String Ball Lights 7.6M/20M Shatterproof Patio Lights For Outside Backyard Porch Garden Decor']

# 保存所有字典到JSON文件
json_file_path = os.path.join(r"D:\subtitle\goods", 'goods_5.json')
with open(json_file_path, 'w', encoding='utf-8') as f:
    json.dump(all_goods_dict, f, ensure_ascii=False, indent=4)